package de.slothsoft.wasbinich.android.free;

import android.content.Context;
import android.os.Build;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import de.slothsoft.wasbinich.android.AbstractActivity;
import de.slothsoft.wasbinich.android.QuestionActivity;

public final class AdvertizingUtil {

    public static final String AD_ID = "ca-app-pub-5424848231753220/5049231195"; //$NON-NLS-1$
    public static final int SHOW_ADS_EVERY_X_PICTURES = 5;

    public static final String DEVELOPMENT_DEVICE = "62B60DA9193A9D5CAA1E8374C18960BF"; //$NON-NLS-1$

    public static void initAdvertizing() {
	QuestionActivity.QUESTION_CALL_BACK = new QuestionAdvertizingCallBack();
	AbstractActivity.ACTIVITY_CALL_BACK = new AdvertizingCallBack();
    }

    public static AdView createAd(AbstractActivity activity, ViewGroup layoutView) {
	AdView result = null;
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && layoutView instanceof GridLayout) {
	    result = AdvertizingUtil.createGridLayoutAd(activity, (GridLayout) layoutView);
	} else if (layoutView instanceof LinearLayout) {
	    result = AdvertizingUtil.createLinearLayoutAd(activity, (LinearLayout) layoutView);
	} else if (layoutView instanceof RelativeLayout) {
	    result = AdvertizingUtil.createRelativeLayoutAd(activity, (RelativeLayout) layoutView);
	}

	if (result != null) {
	    layoutView.addView(result);
	}
	return result;
    }

    public static AdView createGridLayoutAd(AbstractActivity activity, GridLayout layout) {
	layout.setRowCount(layout.getRowCount() + 1);

	AdView result = AdvertizingUtil.createAdView(activity, AdSize.FULL_BANNER);
	GridLayout.LayoutParams params = new GridLayout.LayoutParams();
	params.columnSpec = GridLayout.spec(0, layout.getColumnCount(), GridLayout.CENTER);
	params.rowSpec = GridLayout.spec(layout.getRowCount() - 1, GridLayout.BOTTOM);
	result.setLayoutParams(params);

	return result;
    }

    public static AdView createLinearLayoutAd(AbstractActivity activity, LinearLayout layout) {
	return AdvertizingUtil.createAdView(activity, AdSize.BANNER);
    }

    public static AdView createRelativeLayoutAd(AbstractActivity activity, RelativeLayout layout) {
	AdView result = AdvertizingUtil.createAdView(activity, AdSize.BANNER);
	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
		LayoutParams.WRAP_CONTENT);
	params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
	params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
	result.setLayoutParams(params);
	return result;
    }

    protected static AdView createAdView(Context context, AdSize adSize) {
	AdView adView = new AdView(context);
	adView.setAdUnitId(AD_ID);
	adView.setAdSize(adSize);

	adView.loadAd(creatAdRequest());
	return adView;
    }

    public static InterstitialAd createPopupAd(Context context) {
	InterstitialAd interstitial = new InterstitialAd(context);
	interstitial.setAdUnitId(AD_ID);
	interstitial.loadAd(creatAdRequest());
	return interstitial;
    }

    public static AdRequest creatAdRequest() {
	return new AdRequest.Builder().addKeyword("game").addTestDevice(AdRequest.DEVICE_ID_EMULATOR)// .addTestDevice(DEVELOPMENT_DEVICE) //$NON-NLS-1$
		.build();
    }

    private AdvertizingUtil() {
	// hide me
    }

}
